package com.xjoyt.pay.master.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import com.xjoyt.pay.master.dao.MerchantChannelRepositoryCustom;
import com.xjoyt.pay.master.po.MerchantChannel;
import com.xjoyt.pay.repository.Repository;
import com.xjoyt.pay.vo.PayType;
import com.xjoyt.pay.vo.TradeType;

public interface MerchantChannelRepository extends Repository<MerchantChannel>, MerchantChannelRepositoryCustom {

	@Query(" select a from MerchantChannel a where a.merchant.id = ?1 and a.channel.id = ?2 ")
	List<MerchantChannel> findAllByMerchantIdAndChannelId(String merchantId, String channelId);

	MerchantChannel findOneByKeyAndNo(String key, String no);

	List<MerchantChannel> findAllByMerchantId(String merchantId);

	// @Query(" select a from MerchantChannel a where a.merchant.id = ?1 and
	// a.channel.id = ?2 ")
	// MerchantChannel findOneByMerchantIdAndChannelId(String merchantId, String
	// channelId);

	Page<MerchantChannel> findAllByMerchantIdAndChannelIdAndEnabledTrue(String merchantId, String channelId,
			Pageable pageable);

	/**
	 * 获取可支付的商户通道
	 * 
	 * @param merchantId
	 * @param type
	 * @return
	 */
	@Query(" SELECT a from MerchantChannel a join a.rates r join a.channel c join c.channels p "
			+ " where a.enabled = true and c.enabled = true and r.rate > 0 "
			+ " and a.merchant.id = ?1 and p.payType = ?2 and r.type = ?2 and p.tradeType = ?3 "
			+ " ORDER BY c.weight DESC , p.weight DESC ")
	List<MerchantChannel> findAllByPay(String merchantId, PayType payType, TradeType tradeType);
}
